//
//  Copyright 2015 XiaoJSoft Studio.
//  Copyright 2015 Harbin Institute of Technology(HIT), Software Engineering Department.
//
//  Use of this source code is governed by a proprietary license. You can not read, change or
//  redistribute this source code unless you have a written authorization from the copyright
//  holder listed above.
//

(function() {
    function LoadScripts() {
        var child_nodes = [];
        var head_nodes = document.head.childNodes;

        //  Get all script nodes that should be loaded.
        for (var i = 0; i < head_nodes.length; ++i) {
            var node = head_nodes[i];
            if (node.nodeName == "SCRIPT" && node.type == "text/xjdesktop-script") {
                child_nodes.push(node);
            }
        }

        //  Evaluate each script node by re-adding them to the head of the document.
        child_nodes.forEach(function(node) {
            var script = document.createElement("script");

            //  Set the script type and text.
            script.type = "text/javascript";
            script.text = node.text;

            //  Add the new script node and remove the old one.
            document.head.removeChild(node);
            document.head.appendChild(script);
        });
    }

    function LoadInProgress() {
        //  Poll and load the scripts when the page has been loaded.
        if (document.readyState == "complete") {
            LoadScripts();
        } else {
            setTimeout(LoadInProgress, 1);
        }
    }

    //  Start polling.
    setTimeout(LoadInProgress, 1);
})();
